/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dg.gaming.client.fx.gui;

import com.dg.gaming.api.common.msg.MsgConnectTable;
import com.dg.gaming.api.common.util.GameProperties;
import com.dg.gaming.client.IClient;
import com.dg.gaming.client.TableConnection;
import java.net.Socket;

/**
 *
 * @author dito
 */
public class ConnectionUtil {

    private String server,  tableId;
    private Long username;
    private int port;
    private IClient iClient;
    private TableConnection conn = null;

    public ConnectionUtil(IClient iClient, String server,
            int port,
            Long username,
            String tableId) {


        // Set fields
        this.server = server;
        this.port = port;
        this.username = username;
        this.tableId = tableId;
        this.iClient = iClient;

    }

    public void setIClient(IClient iClient) {
        this.iClient = iClient;
    }

    public IClient getIClient() {
        return iClient;
    }

    public void connect() throws Exception{

        Socket socket = new Socket(server, port);
        // Create a new connection thread to handle communication
        this.conn = new TableConnection(socket, username, iClient);
        new Thread(conn).start(); // Start the thread

        // Create a connect message.
        MsgConnectTable commConnect = null;
//        if (password.equals("")) {
//            commConnect = new MsgGameConnect
//                          (username, GameProperties.getGameID());
//        } else {
//            commConnect = new MsgGameConnect
//                          (username, password, GameProperties.getGameID());
//        }
        commConnect = new MsgConnectTable(Long.parseLong(tableId), username,
                Long.parseLong(GameProperties.getGameID()));
        // Send connect message to the server.
        conn.send(commConnect);

    }

    public TableConnection getConn() {
        return conn;
    }
    
}
